<?php

    /**
     *  Criteria for filtering products by options 
     */
	function critere_ecatalogue_filter_options_dist($idb, &$boucles, $crit) {	   
	   $boucle = &$boucles[$idb];
       $id_table = $boucle->id_table;
       
       if (/*!_request('id_options') || */$id_table!='articles')
           return;
       
       $k = count($boucle->join)+1;
       // Тут не использовал стандартный индекс L$k, потому что SPIP затирает этот элемент массива при джойне в букле к табличке spip_ecatalogue_products
       $boucle->join["FILTER_OPTIONS"] = array($id_table, 'id_article', 'id_article'/*, 'FILTER_OPTIONS.id_option=4'*/); // Первый элемент - таблица, второй - поле по которому джойнится, третий, необязательный - поле из другой таблицы по которому джойнится, четвертый - еще какое то условие которое будет дописано после AND
       $boucle->from["FILTER_OPTIONS"] = 'spip_ecatalogue_articles_options';
       
       // $boucle->hash позволяет выполнять динамические вычисления (не кешируемые)
       //$boucle->hash = 'var_dump(@$Pile[0]["id_options"]); exit;';       
       
       //$boucle->where[]= array("'IN'", "'FILTER_OPTIONS.id_option'", "'(".implode(",",_request('id_options')).")'");
	   
	   $boucle->hash .= '$where_array = critere_ecatalogue_filter_options_create_where_array();';
       if ('$where_array')
           $boucle->where[] = '$where_array';         
		 
		//$boucle->hash .= '$where_arrays = critere_ecatalogue_filter_options_create_where_arrays();';		
		//$where_arrays = $boucle->where;
		//$boucle->where = '$where_arrays';
		//foreach ($where_arrays as $where_array) {
			//$boucle->where[] = $where_array;
		//}			
		//$where_arrays = '$where_arrays';
		//foreach ($where_arrays as $where_array) {
			//$boucle->where[] = $where_array;
		//}
		//$boucle->where[] = $where_arrays;
		//$boucle->where = array_merge($boucle->where, (array)'$where_arrays');
		//critere_ecatalogue_filter_options_create_where_arrays($boucle);
		
		//$id_options = '@$Pile[0]["id_options"]';		
		//foreach ($id_options as $id_option) {
			//$boucle->hash .= '$where_array = critere_ecatalogue_filter_options_create_where_array2('.$id_option.');';
			//$boucle->where[] = '$where_array';
		//}
		
		//$boucle->hash .= '$where_array = critere_ecatalogue_filter_options_create_where_array2();';
		//$boucle->where[] = '$where_array';
    }
    
    function critere_ecatalogue_filter_options_create_where_array() {
        //var_dump(_request('id_options')); exit("aaa!!!");
        if (!_request('id_options'))
            return '';
        return array("IN", "FILTER_OPTIONS.id_option", "(".implode(",",_request('id_options')).")");
    }
	
	function critere_ecatalogue_filter_options_create_where_array2($id_option) {        
        if (!_request('id_options'))
            return '';		
		$id_options = _request('id_options');
		
		if (count($id_options) < 2)
			return array("=", "FILTER_OPTIONS.id_option", $id_options[0]);
        
		$where_array = array("AND", array("=", "FILTER_OPTIONS.id_option", $id_options[0]));
		for ($i=1;$i<count($id_options);$i++) {
			$where_array = array("AND", $where_array, array("=", "FILTER_OPTIONS.id_option", $id_options[$i]));
		}
		return $where_array;
    }
    
    function critere_ecatalogue_filter_options_create_where_string() {
        $id_options = _request('id_options');
        if (!$id_options || count($id_options) < 2) return "";
        $s = "";
        for ($i = 1; $i < count($id_options); $i++) {
            //$s .= " AND " . ;
        }
    }
	
	function critere_ecatalogue_filter_options_create_where_arrays(/*&$boucle*/) {
		if (!_request('id_options'))
            return;
		$where_arrays = array();	
		foreach (_request('id_options') as $id_option) {
			$where_arrays[] = array("=", "FILTER_OPTIONS.id_option", $id_option);
		}
		//$boucle->where = array_merge($boucle->where, $where_arrays);		
		return $where_arrays;
	}